package client;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;

import common.IAuctionListener;
import common.IAuctionServer;
import common.Item;

public class Client extends JFrame implements IAuctionListener {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	
	private static IAuctionServer server;
	private JTextField textField_2;
	private JTextPane textPane_1;
	private JTable table;
	private ItemTableModel model;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JTextField textField_3;
	private IAuctionListener stub;
	private JTextField textField_4;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		launchServer();
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Client frame = new Client();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Client() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 693, 397);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(494, 91, 170, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(494, 203, 170, 20);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		btnNewButton = new JButton("Dodaj item");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					server.placeItemForBid(textField_4.getText(), textField.getText(), textPane_1.getText(), Double.parseDouble(textField_2.getText()), Integer.parseInt(textField_1.getText()));
					registerListener(textField.getText());
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				model.update(getAllItems());
			}
		});
		btnNewButton.setBounds(497, 325, 170, 23);
		contentPane.add(btnNewButton);
		
		JLabel lblNewLabel = new JLabel("Nazwa");
		lblNewLabel.setBounds(494, 66, 46, 14);
		contentPane.add(lblNewLabel);
		
		JLabel lblCzasTrwaniaAukcji = new JLabel("Czas trwania aukcji");
		lblCzasTrwaniaAukcji.setBounds(494, 178, 108, 14);
		contentPane.add(lblCzasTrwaniaAukcji);
		
		textField_2 = new JTextField();
		textField_2.setBounds(494, 147, 170, 20);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		JLabel lblCenaStartowa = new JLabel("Cena startowa");
		lblCenaStartowa.setBounds(494, 122, 84, 14);
		contentPane.add(lblCenaStartowa);
		
		textPane_1 = new JTextPane();
		textPane_1.setBounds(494, 259, 170, 55);
		contentPane.add(textPane_1);
		
		JLabel lblOpis = new JLabel("Opis");
		lblOpis.setBounds(494, 234, 46, 14);
		contentPane.add(lblOpis);
		
		table = new JTable();
		table.setBounds(10, 14, 474, 187);
		
		model = new ItemTableModel();
		model.update(getAllItems());
		table.setModel(model);
		contentPane.add(table);
		
		textField_3 = new JTextField();
		textField_3.setBounds(10, 212, 143, 20);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		JButton btnNewButton_1 = new JButton("Place bid");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRow()!=-1) {
					Item item = model.getRecordAt(table.getSelectedRow());
					try {
						registerListener(item.getName());
						server.bidOnItem(textField_4.getText(), item.getName(), Double.parseDouble(textField_3.getText()));
					} catch (NumberFormatException | RemoteException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		btnNewButton_1.setBounds(163, 212, 203, 23);
		contentPane.add(btnNewButton_1);
		
		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				model.update(getAllItems());
			}
		});
		btnRefresh.setBounds(384, 325, 89, 23);
		contentPane.add(btnRefresh);
		
		textField_4 = new JTextField();
		textField_4.setBounds(494, 35, 170, 20);
		contentPane.add(textField_4);
		textField_4.setColumns(10);
		
		JLabel lblUser = new JLabel("User");
		lblUser.setBounds(494, 14, 46, 14);
		contentPane.add(lblUser);
		
		JRadioButton rdbtnNone = new JRadioButton("None");
		rdbtnNone.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//brak sytuacji
			}
		});
		rdbtnNone.setBounds(28, 260, 57, 23);
		contentPane.add(rdbtnNone);
		
		JLabel lblStrategia = new JLabel("Strategia:");
		lblStrategia.setBounds(10, 243, 75, 14);
		contentPane.add(lblStrategia);
		
		JRadioButton rdbtnOutbid = new JRadioButton("Outbid");
		rdbtnOutbid.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// taka sytuacja
			}
		});
		rdbtnOutbid.setBounds(28, 286, 109, 23);
		contentPane.add(rdbtnOutbid);
		
		JRadioButton rdbtnLastMinute = new JRadioButton("Last Minute");
		rdbtnLastMinute.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//taka sytuacja2
			}
		});
		rdbtnLastMinute.setBounds(28, 312, 109, 23);
		contentPane.add(rdbtnLastMinute);
		
		try {
			stub = (IAuctionListener) UnicastRemoteObject.exportObject(this, 0);
		}
		catch (RemoteException e) {
			e.printStackTrace();
		}
		
	}
	
	private static void launchServer() {
		Registry registry;
		try {
			registry = LocateRegistry.getRegistry(10778);
			server = (IAuctionServer) registry.lookup("Server");
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	private List<Item> getAllItems() {
		List<Item> itemy = null;
		
		try {
			itemy = server.getItems();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return itemy;
	}
	
	private void registerListener(String itemName) {
		try {
			server.registerListener(stub, itemName);
		} catch (RemoteException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
	}

	@Override
	public void update(Item item) throws RemoteException {
		if (item.getAuctionTime()==0) {
			model.remove(item);
		} else {
			model.addOrUpdate(item);
		}
	}
}
